package com.rjwh.dingdong.client.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.rjwh.dingdong.client.server.HttpWebServer;
import com.rjwh.dingdong.client.util.FileUtils;

/**
 * 本地Web服务器
 */
public class HttpWebCoreService extends Service {

    public static final int PORT = 7766;
    public static final String LOCALHOST = "http://0.0.0.0";
    public static final String DEFAULT_URL = HttpWebCoreService.LOCALHOST + ":" + HttpWebCoreService.PORT + "/";


    public static final String GAME_URL = DEFAULT_URL + ".game/%s/map/index.html?userid=%s&babyid=%s&platform=android&version=%s&udid=%s&token=%s&timestamp=%s&bookid=%s";
    public static final String HUIBEN_URL = DEFAULT_URL + ".drwabook/%s/index.html?userid=%s&babyid=%s&platform=android&version=%s&udid=%s&token=%s&timestamp=%s&bookid=%s";
    //个体游戏路径
    public static final String GAME_ITEM_URL = DEFAULT_URL + ".game/%s/%s/index.html?userid=%s&babyid=%s&platform=android&version=%s&udid=%s&token=%s&timestamp=%s&bookid=%s&lessonid=%s&gamepath=%s&gameid=%s";
    //课程路径
    public static final String GAME_LESSON_URL = DEFAULT_URL + ".game/%s/map/index.html?userid=%s&babyid=%s&platform=android&version=%s&udid=%s&token=%s&timestamp=%s&bookid=%s&lessonid=%s";
    private HttpWebServer webServer;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            if (webServer != null) {
                webServer.close();
                webServer = null;
            }
        }catch(Exception e){

        }
        if(webServer == null){
            webServer = new HttpWebServer(PORT, FileUtils.getDirectory());
        }
        webServer.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if(webServer!=null)webServer.close();
        super.onDestroy();
    }

}
